/**
 * @author mapeng
 * @version 2017-05-10
 */

(function(doc, win) {
  var pageVariables = {
    stepOne4Wangshang: {},
    weixinAccount: {}
  };

  $(doc).on("pageInit", "#weixinOpenStep2Page4Wangshang", function(
    e,
    pageId,
    $page,
    isBack,
    isForward
  ) {
    if (isForward) {
      handlePageForward();
      return;
    }
    if (isBack) return;

    init4Page();
  });
  bindEvents4Page();

  //页面前进
  function handlePageForward() {
    var params = CommonUtil.takeOutParamObject() || {};
    pageVariables.stepOne4Wangshang = params.stepOne4Wangshang;

    //重置账户类型
    init4AccountType();

    listen4Submit();
  }

  //页面初始化
  function init4Page() {
    var params = CommonUtil.takeOutParamObject() || {};
    var editFlag = false;
    if (params && params.stepOne4Wangshang) {
      editFlag = params.stepOne4Wangshang.shopId > 0;
    }
    pageVariables.stepOne4Wangshang = params.stepOne4Wangshang;
    //获取帐号信息用作编辑
    pageVariables.weixinAccount = params.weixinAccount;

    //账户类型
    init4AccountType();

    //开户行地区
    init4AccountDistrict(editFlag ? pageVariables.weixinAccount : {});

    //开户主银行
    // init4AccountMainBank(pageVariables.stepOne4Wangshang.type == Constants.weixin.OPERATE_TYPE.edit ? pageVariables.weixinAccount : null);
    //编辑
    edit4Page(editFlag);
  }

  //初始化页面值：编辑
  function edit4Page(editFlag) {
    var weixinAccount = pageVariables.weixinAccount;
    //支行名称
    $("#weixinOpenStep2Page4Wangshang input[name='branchBank']").val(
      weixinAccount.branchName
    );

    //联行号
    if (
      pageVariables.stepOne4Wangshang.merchantType ==
      Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
    ) {
      //企业商户
      $("#weixinOpenStep2Page4Wangshang input[name='contactLine']").val(
        weixinAccount.contactLine || ""
      );
    }

    //银行帐号
    $("#weixinOpenStep2Page4Wangshang input[name='accountCode']").val(
      weixinAccount.bankCardNo || ""
    );

    //开户名
    $("#weixinOpenStep2Page4Wangshang input[name='accountName']").val(
      weixinAccount.bankCertName || ""
    );

    //开户人身份证号、开户人地址
    if (
      pageVariables.stepOne4Wangshang.merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.nature_person ||
      pageVariables.stepOne4Wangshang.merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.person_business
    ) {
      //自然人、个体工商户
      if (weixinAccount && weixinAccount.certNo) {
        $("#weixinOpenStep2Page4Wangshang input[name='accounterCard']").val(
          weixinAccount.certNo
        );
      }
      if (weixinAccount && weixinAccount.cardHolderAddress) {
        $("#weixinOpenStep2Page4Wangshang input[name='accounterAddress']").val(
          weixinAccount.cardHolderAddress
        );
      }
    }
    // //邀请码
    // if (weixinAccount && weixinAccount.invitationCode) {
    //   $("#weixinOpenStep2Page4Wangshang input[name='invitationCode']")
    //     .val(weixinAccount.invitationCode)
    //     .closest("li")
    //     .hide();
    //   $("#weixinOpenStep2Page4Wangshang #divInvitationCode")
    //     .text(weixinAccount.invitationCode)
    //     .closest("li")
    //     .removeAttr("style");
    // }

    listen4Submit();
  }

  //账户类型
  function init4AccountType() {
    var accountType = "";
    if (
      pageVariables.stepOne4Wangshang.merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.nature_person ||
      pageVariables.stepOne4Wangshang.merchantType ==
        Constants.weixin_wangshang.MERCHANT_TYPE.person_business
    ) {
      //自然人、个体工商户
      accountType = Constants.weixin_wangshang.ACCOUNT_TYPE.person;
    } else if (
      pageVariables.stepOne4Wangshang.merchantType ==
      Constants.weixin_wangshang.MERCHANT_TYPE.enterprise_business
    ) {
      //企业商户
      accountType = Constants.weixin_wangshang.ACCOUNT_TYPE.company;
    }

    $(
      "#weixinOpenStep2Page4Wangshang div[name='accountType'] input[type='text']"
    ).val(Constants.weixin_wangshang.getAccountTypeText(accountType));
    $(
      "#weixinOpenStep2Page4Wangshang div[name='accountType'] input[type='hidden']"
    ).val(accountType);

    setFields4AccountType(accountType);
  }

  //可见字段：账户类型
  //个人账户：开户人身份证号、开户人地址
  //公司账户：联行号
  function setFields4AccountType(accountType) {
    if (accountType == Constants.weixin_wangshang.ACCOUNT_TYPE.person) {
      //对私账户
      // $("#weixinOpenStep2Page4Wangshang input[name='contactLine']").parents("li").css("display","none");	//联行号
      $("#weixinOpenStep2Page4Wangshang input[name='accounterCard']")
        .parents("li")
        .css("display", ""); //开户人姓名
      $("#weixinOpenStep2Page4Wangshang input[name='accounterAddress']")
        .parents("li")
        .css("display", ""); //开户人地址
    } else if (accountType == Constants.weixin_wangshang.ACCOUNT_TYPE.company) {
      //对公账户
      // $("#weixinOpenStep2Page4Wangshang input[name='contactLine']").parents("li").css("display","");
      $("#weixinOpenStep2Page4Wangshang input[name='accounterCard']")
        .parents("li")
        .css("display", "none");
      $("#weixinOpenStep2Page4Wangshang input[name='accounterAddress']")
        .parents("li")
        .css("display", "none");
    }
  }

  //开户行地区
  function init4AccountDistrict(weixinAccount) {
    CityUtil.getProvinceCityHtml(function(ul) {
      $("#weixinOpenStep2Page4Wangshang div[name='bankDistrict']").prepend(ul);
      $("#weixinOpenStep2Page4Wangshang div[name='bankDistrict']")
        .children("ul")
        .mobiscroll()
        .treelist({
          theme: "ios",
          display: "bottom",
          lang: "zh",
          placeholder: "请选择",
          formatValue: function(data) {
            if (data && data.length == 2 && data[0] && data[1]) {
              return data.join("-");
            }
            return "请选择";
          },
          onSelect: function(valueText, inst) {
            var codeArray = [];
            var $ul = ($this = $(this));
            $.each(valueText.split("-"), function(index, value) {
              var $li = $ul.children("li[data-val='" + value + "']");
              codeArray.push($li.attr("data-code")); //data会进行类型转换
              $ul = $li.children("ul").eq(0);
            });
            $this
              .parent()
              .find("input[type='hidden']")
              .val(codeArray.join(","));
            init4AccountMainBank(weixinAccount, true);
            listen4Submit();
          }
        });

      //设置初始值
      if (weixinAccount) {
        $(
          "#weixinOpenStep2Page4Wangshang div[name='bankDistrict']>ul"
        ).mobiscroll(
          "setArrayVal",
          [weixinAccount.branchProvinceName, weixinAccount.branchCityName],
          true
        );
        $(
          "#weixinOpenStep2Page4Wangshang div[name='bankDistrict']>input[type='hidden']"
        ).val(weixinAccount.branchProvince + "," + weixinAccount.branchCity);
        init4AccountMainBank(weixinAccount);
        listen4Submit();
      }
    });
  }

  //开户主银行
  function init4AccountMainBank(weixinAccount, hasSelectedDistrick) {
    //销毁组件
    if (
      $("#weixinOpenStep2Page4Wangshang div[name='mainBank']>ul").length > 0
    ) {
      $("#weixinOpenStep2Page4Wangshang div[name='mainBank']>ul").mobiscroll(
        "destroy"
      );
      $("#weixinOpenStep2Page4Wangshang div[name='mainBank']>ul").remove();
      $(
        "#weixinOpenStep2Page4Wangshang div[name='mainBank']>input[type='hidden']"
      ).val("");
    }
    if (
      $("#weixinOpenStep2Page4Wangshang div[name='branchBank']>ul").length > 0
    ) {
      $("#weixinOpenStep2Page4Wangshang div[name='branchBank']>ul").mobiscroll(
        "destroy"
      );
      $("#weixinOpenStep2Page4Wangshang div[name='branchBank']>ul").remove();
      $(
        "#weixinOpenStep2Page4Wangshang div[name='branchBank']>input[type='hidden']"
      ).val("");
      $("#weixinOpenStep2Page4Wangshang input[name='contactLine']").val(""); //联行号
    }
    var bankDistrict = $(
      "#weixinOpenStep2Page4Wangshang div[name='bankDistrict'] input[type='hidden']"
    ).val(); //开户行地区
    CommonUtil.sendJsonpRequest({
      // testApi.testGetBankList({
      url: App.serverUrl + "/app/wxAccountCloudBox/getBanks.do",
      params: { bankDistrict: bankDistrict },
      success: function(result) {
        if (result.success.toString() == "true") {
          //success

          var bankList = result.resultObject;
          if (bankList && bankList.length > 0) {
            var bankListHtml = "<ul style='display:none;'>";
            $.each(bankList, function(index, element) {
              bankListHtml +=
                "<li data-code='" +
                element.bankCode +
                "' data-val='" +
                element.bankName +
                "'>" +
                element.bankName +
                "</li>";
            });
            bankListHtml += "</ul>";
            $("#weixinOpenStep2Page4Wangshang div[name='mainBank']").prepend(
              bankListHtml
            );

            $("#weixinOpenStep2Page4Wangshang div[name='mainBank']")
              .children("ul")
              .mobiscroll()
              .treelist({
                theme: "ios",
                display: "bottom",
                lang: "zh",
                placeholder: "请选择",
                onSelect: function(valueText, inst) {
                  var codeArray = [];
                  var $ul = ($this = $(this));
                  $.each(valueText.split("-"), function(index, value) {
                    var $li = $ul.children("li[data-val='" + value + "']");
                    codeArray.push($li.attr("data-code")); //data会进行类型转换
                    $ul = $li.children("ul").eq(0);
                  });
                  $this
                    .parent()
                    .find("input[type='hidden']")
                    .val(codeArray.join(","));
                  init4AccountBranchBank(weixinAccount, true);
                  listen4Submit();
                }
              });
          }

          //设置初始值
          if (weixinAccount) {
            if (weixinAccount.bankId && !hasSelectedDistrick) {
              $(
                "#weixinOpenStep2Page4Wangshang div[name='mainBank']>ul"
              ).mobiscroll("setVal", weixinAccount.bankCode, true);
              $(
                "#weixinOpenStep2Page4Wangshang div[name='mainBank']>input[type='hidden']"
              ).val(weixinAccount.bankId);
            }
            init4AccountBranchBank(weixinAccount, false);
            listen4Submit();
          }
        } else {
          //business error
          DialogUtil.dialogWarn(result.msg);
        }
      },
      error: function() {
        //not 200
        DialogUtil.dialogWarn("请求失败，请稍候重试");
      }
    });
  }

  //开户支行
  function init4AccountBranchBank(weixinAccount, hasSelectedBank) {
    //销毁组件
    if (
      $("#weixinOpenStep2Page4Wangshang div[name='branchBank']>ul").length > 0
    ) {
      $("#weixinOpenStep2Page4Wangshang div[name='branchBank']>ul").mobiscroll(
        "destroy"
      );
      $("#weixinOpenStep2Page4Wangshang div[name='branchBank']>ul").remove();
      $(
        "#weixinOpenStep2Page4Wangshang div[name='branchBank']>input[type='hidden']"
      ).val("");
      $("#weixinOpenStep2Page4Wangshang input[name='contactLine']").val(""); //联行号
    }
    var bankDistrict = $(
      "#weixinOpenStep2Page4Wangshang div[name='bankDistrict'] input[type='hidden']"
    ).val(); //开户行地区
    var bankId = $(
      "#weixinOpenStep2Page4Wangshang div[name='mainBank']>input[type='hidden']"
    ).val(); //开户银行Id
    if (bankId) {
      CommonUtil.sendJsonpRequest({
        // apiTest.testGetBankList({
        url:
          App.serverUrl +
          "/app/wxAccountCloudBox/queryMerchantBankBranchAnt.do",
        params: { bankDistrict: bankDistrict, bank: bankId },
        success: function(result) {
          if (result.success.toString() == "true") {
            //success

            //初始化组件
            var bankList = result.resultObject;
            var bankListHtml = "<ul style='display:none;'>";
            $.each(bankList, function(index, element) {
              bankListHtml +=
                "<li data-code='" +
                element.contactLine +
                "' data-val='" +
                element.bankName +
                "'>" +
                element.bankName +
                "</li>";
            });
            bankListHtml += "</ul>";
            $("#weixinOpenStep2Page4Wangshang div[name='branchBank']").prepend(
              bankListHtml
            );

            $("#weixinOpenStep2Page4Wangshang div[name='branchBank']>ul")
              .mobiscroll()
              .treelist({
                theme: "ios",
                display: "bottom",
                lang: "zh",
                placeholder: "请选择",
                onSelect: function(valueText, inst) {
                  var codeArray = [];
                  var $ul = ($this = $(this));
                  $.each(valueText.split("-"), function(index, value) {
                    var $li = $ul.children("li[data-val='" + value + "']");
                    codeArray.push($li.attr("data-code")); //data会进行类型转换
                    // $ul = $li.children("ul").eq(0);
                    $(
                      "#weixinOpenStep2Page4Wangshang input[name='contactLine']"
                    ).val($li.attr("data-code")); //联行号
                  });
                  $this
                    .parent()
                    .find("input[type='hidden']")
                    .val(valueText);
                  listen4Submit();
                }
              });
            if (weixinAccount && !hasSelectedBank) {
              //设置值
              $(
                "#weixinOpenStep2Page4Wangshang div[name='branchBank']>ul"
              ).mobiscroll("setVal", weixinAccount.branchName, true);
              $(
                "#weixinOpenStep2Page4Wangshang div[name='branchBank']>input[type='hidden']"
              ).val(weixinAccount.branchName);
              $("#weixinOpenStep2Page4Wangshang input[name='contactLine']").val(
                weixinAccount.contactLine
              ); //联行号
              listen4Submit();
            }
          } else {
            //business error
            DialogUtil.dialogWarn(result.msg);
          }
        },
        error: function() {
          //not 200
          DialogUtil.dialogWarn("请求失败，请稍候重试");
        }
      });
    } else {
      //初始化空组件
      var bankList = [{ contactLine: "", bankName: "" }];
      var bankListHtml = "<ul style='display:none;'>";
      $.each(bankList, function(index, element) {
        bankListHtml +=
          "<li data-code='" +
          element.contactLine +
          "' data-val='" +
          element.bankName +
          "'>" +
          element.bankName +
          "</li>";
      });
      bankListHtml += "</ul>";
      $("#weixinOpenStep2Page4Wangshang div[name='branchBank']").prepend(
        bankListHtml
      );

      $("#weixinOpenStep2Page4Wangshang div[name='branchBank']>ul")
        .mobiscroll()
        .treelist({
          theme: "ios",
          display: "bottom",
          lang: "zh",
          placeholder: "请选择",
          disabled: "true"
          // "onSelect" : function(valueText,inst){
          // 	var codeArray = [];
          // 	var $ul = $this = $(this);
          // 	$.each(valueText.split("-"),function(index,value){
          // 		var $li = $ul.children("li[data-val='"+value+"']");
          // 		codeArray.push($li.attr("data-code"));  //data会进行类型转换
          // 		// $ul = $li.children("ul").eq(0);
          // 		$("#weixinOpenStep2Page4Wangshang input[name='contactLine']").val($li.attr("data-code"));	//联行号
          // 	});
          // 	$this.parent().find("input[type='hidden']").val(valueText);
          // 	listen4Submit();
          // }
        });
    }
  }

  //提交：输入监听
  function listen4Submit() {
    var accountType = $(
      "#weixinOpenStep2Page4Wangshang div[name='accountType'] input[type='hidden']"
    ).val(); //账户类型
    var bankDistrict = $(
      "#weixinOpenStep2Page4Wangshang div[name='bankDistrict'] input[type='hidden']"
    ).val(); //开户行所在地区
    var mainBank = $(
      "#weixinOpenStep2Page4Wangshang div[name='mainBank'] input[type='hidden']"
    ).val(); //开户主行
    var branchBank = $(
      "#weixinOpenStep2Page4Wangshang div[name='branchBank']>input[type='hidden']"
    ).val(); //支行名称
    if (branchBank) {
      branchBank = branchBank.trim();
    }
    var accountCode = $(
      "#weixinOpenStep2Page4Wangshang input[name='accountCode']"
    )
      .val()
      .trim(); //银行帐号
    var accountName = $(
      "#weixinOpenStep2Page4Wangshang input[name='accountName']"
    )
      .val()
      .trim(); //开户名

    if (
      accountType &&
      bankDistrict &&
      mainBank &&
      branchBank &&
      accountCode &&
      accountName
    ) {
      if (accountType == Constants.weixin_wangshang.ACCOUNT_TYPE.person) {
        //对私账户
        var accounterCard = $(
          "#weixinOpenStep2Page4Wangshang input[name='accounterCard']"
        )
          .val()
          .trim(); //开户人身份证号
        var accounterAddress = $(
          "#weixinOpenStep2Page4Wangshang input[name='accounterAddress']"
        )
          .val()
          .trim(); //开户人地址
        if (accounterCard && accounterAddress) {
          $("#weixinOpenStep2Page4Wangshang .header li.right").removeClass(
            "disabled"
          );
        } else {
          $("#weixinOpenStep2Page4Wangshang .header li.right").addClass(
            "disabled"
          );
        }
      } else if (
        accountType == Constants.weixin_wangshang.ACCOUNT_TYPE.company
      ) {
        //对公账户
        var contactLine = $(
          "#weixinOpenStep2Page4Wangshang input[name='contactLine']"
        )
          .val()
          .trim(); //联行号
        if (contactLine) {
          $("#weixinOpenStep2Page4Wangshang .header li.right").removeClass(
            "disabled"
          );
        } else {
          $("#weixinOpenStep2Page4Wangshang .header li.right").addClass(
            "disabled"
          );
        }
      }
    } else {
      $("#weixinOpenStep2Page4Wangshang .header li.right").addClass("disabled");
    }
  }

  //页面事件绑定
  function bindEvents4Page() {
    //返回
    $(document).on(
      "click",
      "#weixinOpenStep2Page4Wangshang .header .back",
      function(e) {
        var cache = CommonUtil.takeOutParamObject() || {};
        cache.handleForward = "forward";
        CommonUtil.injectParamObject(cache);
        $.router.back();
      }
    );

    //提交：监听输入
    var listenFormStr =
      "#weixinOpenStep2Page4Wangshang input[name='branchBank']," +
      "#weixinOpenStep2Page4Wangshang input[name='contactLine']," +
      "#weixinOpenStep2Page4Wangshang input[name='accountCode']," +
      "#weixinOpenStep2Page4Wangshang input[name='accountName']," +
      "#weixinOpenStep2Page4Wangshang input[name='accounterCard']," +
      "#weixinOpenStep2Page4Wangshang input[name='accounterAddress']";
    $(document).on("input", listenFormStr, function(e) {
      listen4Submit();
    });
    $(document).on(
      "clearInput",
      "#weixinOpenStep2Page4Wangshang .zhk-list_gb",
      function(e) {
        listen4Submit();
      }
    );

    //提交
    $(document).on(
      "click",
      "#weixinOpenStep2Page4Wangshang .header a[name='submit']",
      function(e) {
        if (
          $(this)
            .parents(".right")
            .hasClass("disabled")
        )
          return;

        submit();
      }
    );
  }

  //提交
  function submit() {
    //表单参数
    var accountType = $(
      "#weixinOpenStep2Page4Wangshang div[name='accountType'] input[type='hidden']"
    ).val(); //账户类型
    var bankDistrict = $(
      "#weixinOpenStep2Page4Wangshang div[name='bankDistrict'] input[type='hidden']"
    ).val(); //开户行所在地区
    var bankDistrictText = $(
      "#weixinOpenStep2Page4Wangshang div[name='bankDistrict']>ul"
    ).mobiscroll("getArrayVal");
    var mainBank = $(
      "#weixinOpenStep2Page4Wangshang div[name='mainBank']>input[type='text']"
    ).val(); //开户主行
    var mainBankId = $(
      "#weixinOpenStep2Page4Wangshang div[name='mainBank'] input[type='hidden']"
    ).val(); // 开户银行ID
    // var branchBank = $("#weixinOpenStep2Page4Wangshang input[name='branchBank']").val().trim();	//支行名称
    var branchBank = $(
      "#weixinOpenStep2Page4Wangshang div[name='branchBank']>input[type='text']"
    )
      .val()
      .trim(); //支行名称
    var contactLine = ""; //联行号
    var accountCode = $(
      "#weixinOpenStep2Page4Wangshang input[name='accountCode']"
    )
      .val()
      .trim(); //银行帐号
    var accountName = $(
      "#weixinOpenStep2Page4Wangshang input[name='accountName']"
    )
      .val()
      .trim(); //开户名
    var accounterCard = ""; //开户人身份证号
    var accounterAddress = ""; //开户人地址
    // var invitationCode = $(
    //   "#weixinOpenStep2Page4Wangshang input[name='invitationCode']"
    // )
    //   .val()
    //   .trim();

    //校验：账户类型
    if (!accountType) {
      DialogUtil.dialogWarn("请选择账户类型");
      return;
    }

    //校验：开户行地区
    if (!bankDistrict) {
      DialogUtil.dialogWarn("请选择开户行地区");
      return;
    }

    //校验：开户银行
    if (!mainBank) {
      DialogUtil.dialogWarn("请选择开户银行");
      return;
    }

    //校验：支行名称
    if (!branchBank) {
      DialogUtil.dialogWarn("请选择支行名称");
      return;
    }
    if (!/^[\dA-Za-z\u4e00-\u9fa5]+$/.test(branchBank)) {
      DialogUtil.dialogWarn("支行名称仅允许输入中文、英文、数字");
      return;
    }
    if (branchBank.length > 64) {
      DialogUtil.dialogWarn("支行名称长度不能超过64");
      return;
    }

    //校验：联行号
    // if(accountType == Constants.weixin_wangshang.ACCOUNT_TYPE.company){//对公账户
    contactLine = $("#weixinOpenStep2Page4Wangshang input[name='contactLine']")
      .val()
      .trim(); //联行号
    if (contactLine.length == 0) {
      DialogUtil.dialogWarn("请填写联行号");
      return;
    }
    if (!/^\d{1,20}$/.test(contactLine)) {
      DialogUtil.dialogWarn("请填写有效的联行号");
      return;
    }
    // }

    //校验：银行帐号
    if (accountCode.length == 0) {
      DialogUtil.dialogWarn("请填写银行账号");
      return;
    }
    if (!/^\d{1,32}$/.test(accountCode)) {
      DialogUtil.dialogWarn("请填写有效的银行账号");
      return;
    }

    //校验：开户名
    if (!accountName) {
      DialogUtil.dialogWarn("请选择开户人名称");
      return;
    }
    if (!/^[\dA-Za-z\u4e00-\u9fa5\\(\\)（）]+$/.test(accountName)) {
      DialogUtil.dialogWarn("开户人名称不允许包含特殊字符");
      return;
    }
    if (accountName.length > 64) {
      DialogUtil.dialogWarn("开户人名称长度不能超过64");
      return;
    }
    if (accountType == Constants.weixin_wangshang.ACCOUNT_TYPE.person) {
      //对私账户
      if (accountName != pageVariables.stepOne4Wangshang.principal) {
        DialogUtil.dialogWarn("开户名需与负责人名称一致");
        return;
      }
    }
    if (accountType == Constants.weixin_wangshang.ACCOUNT_TYPE.company) {
      //对公账户
      if (accountName != pageVariables.stepOne4Wangshang.merchantName) {
        DialogUtil.dialogWarn("开户名需与门店名称一致");
        return;
      }
    }

    if (accountType == Constants.weixin_wangshang.ACCOUNT_TYPE.person) {
      //对私账户

      accounterCard = $(
        "#weixinOpenStep2Page4Wangshang input[name='accounterCard']"
      )
        .val()
        .trim(); //开户人身份证号
      accounterAddress = $(
        "#weixinOpenStep2Page4Wangshang input[name='accounterAddress']"
      )
        .val()
        .trim(); //开户人地址

      //开户人身份证号
      if (accounterCard.length == 0) {
        DialogUtil.dialogWarn("请填写开户人身份证号码");
        return;
      }
      if (!/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(accounterCard)) {
        DialogUtil.dialogWarn("请填写有效的开户人身份证号码");
        return;
      }
      if (accounterCard != pageVariables.stepOne4Wangshang.idCard) {
        DialogUtil.dialogWarn("开户人身份证号必须与负责人身份证号一致");
        return;
      }

      //校验：开户人地址
      if (!accounterAddress) {
        DialogUtil.dialogWarn("请填写开户人地址");
        return;
      }
      if (!/^[\dA-Za-z\u4e00-\u9fa5]+$/.test(accounterAddress)) {
        DialogUtil.dialogWarn("开户人地址仅允许输入中文、英文、数字");
        return;
      }
      if (accounterAddress.length > 128) {
        DialogUtil.dialogWarn("开户人地址长度不能超过128");
        return;
      }
    }

    //数据组装
    var stepTwo = {
      accountType: accountType, //账户类型
      branchProvince: bankDistrict.split(",")[0], //开户行省份
      branchProvinceName: bankDistrictText[0],
      branchCity: bankDistrict.split(",")[1], //开户行市
      branchCityName: bankDistrictText[1],
      bankCode: mainBank, //开户银行
      bankId: mainBankId, //开户银行ID
      branchName: branchBank, //支行名称
      bankCardNo: accountCode, //银行帐号
      bankCertName: accountName, //开户名
      contactLine: contactLine, //银联号
      //invitationCode: invitationCode //邀请码
    };
    if (accountType == Constants.weixin_wangshang.ACCOUNT_TYPE.person) {
      //对私账户
      stepTwo["certNo"] = accounterCard;
      stepTwo["cardHolderAddress"] = accounterAddress;
    } else if (accountType == Constants.weixin_wangshang.ACCOUNT_TYPE.company) {
      //对公账户
      // stepTwo["contactLine"] = contactLine;
    }
    var submitData = $.extend({}, pageVariables.stepOne4Wangshang, stepTwo);

    // //负责人手机号验证：商户类型为自然人和个体工商户
    // if (
    //   pageVariables.stepOne4Wangshang.merchantType ==
    //     Constants.weixin_wangshang.MERCHANT_TYPE.nature_person ||
    //   pageVariables.stepOne4Wangshang.merchantType ==
    //     Constants.weixin_wangshang.MERCHANT_TYPE.person_business
    // ) {
    //   var cache = CommonUtil.takeOutParamObject() || {};
    //   cache.submitData = submitData;
    //   CommonUtil.injectParamObject(cache);
    //   $.router.load("weixin_open_mobile_verify_wangshang.html", true);
    //   return;
    // }

    //提交：企业商户直接提交
    common.loading("提交中");
    AppUtil.call(
      // testApi.testSubmit(
      AppUtil.APIS.SEND_HTTP_REQUEST,
      {
        type: "POST",
        url: App.serverUrl + "/app/wxAccountCloudBox/apply.do",
        data: JSON.stringify(submitData)
      },
      function(submitResult) {
        common.closeLoading();

        var cache = CommonUtil.takeOutParamObject() || {};
        cache.shopId = pageVariables.stepOne4Wangshang.shopId;
        CommonUtil.injectParamObject(cache);

        if (
          submitResult.success.toString() == "true" &&
          submitResult.resultObject.success.toString() == "true"
        ) {
          //成功
          $.router.load("weixin_open_success_wangshang.html");
        } else {
          //失败
          if(submitResult.msg=="33150000"||submitResult.resultObject.msg=="33150000"){
            DialogUtil.dialogConfirm({
              title: "图片缺失",
              content:"请登录云纵商家中心开通收款账户",
              btnLeft: "知道了"
            });
            return;
          }
         
          var errorMsg = "";
          if (submitResult.success.toString() == "false") {
            errorMsg = submitResult.msg;
          } else if (submitResult.resultObject.success.toString() == "false") {
            errorMsg = submitResult.resultObject.msg;
          }

          var cache = CommonUtil.takeOutParamObject() || {};
          cache.errorMsg = errorMsg;
          CommonUtil.injectParamObject(cache);
          $.router.load("weixin_open_error_wangshang.html");
        }
      }
    );
  }

  /**
   * 测试接口
   * @type {Object}
   */
  var testApi = {
    testSubmit: function(apiName, params, callback) {
      //提交
      callback({
        code: "0",
        msg: "",
        resultObject: {
          code: "0",
          msg: "参数不正确",
          success: false,
          resultObject: ""
        },
        success: true
      });
    },

    testGetBankList: function(options) {
      //获取开户银行列表
      options.success({
        success: true,
        resultObject: [
          { bankCode: "001", bankName: "中国银行" },
          { bankCode: "002", bankName: "中国工商银行" },
          { bankCode: "003", bankName: "中国建设银行" },
          { bankCode: "004", bankName: "中国农业银行" }
        ]
      });
    }
  };
})(document, window);
